﻿using System;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class RadiatorDimension
	{
		public void SetRadiatorDimension(FamilyInstance fi, Autodesk.Revit.DB.Document document, bool isUp = false)
		{
			this.doc = document;
			YJKRevitTools.DeleteTagOfElement(document, BuiltInCategory.OST_MechanicalEquipmentTags, fi);
			FamilySymbol familySymbol = YJKRevitTools.GetFamilySymbol(document, "HY_散热器标记", BuiltInCategory.OST_MechanicalEquipmentTags);
			TagOrientation tagOrientation = 0;
			if (fi.FacingOrientation.IsAlmostEqualTo(new XYZ(1.0, 0.0, 0.0)) || fi.FacingOrientation.IsAlmostEqualTo(new XYZ(-1.0, 0.0, 0.0)))
			{
				tagOrientation = 1;
			}
			if (familySymbol != null)
			{
				IndependentTag independentTag = RevitVersionFuncs.NewTag(document, document.ActiveView, new Reference(fi), false, 0, tagOrientation, this.GetTagLoca(fi, isUp));
				independentTag.ChangeTypeId(new ElementId(Convert.ToInt32(familySymbol.Id)));
				independentTag.HasLeader = false;
				independentTag.TagOrientation = this.GetTagOrientation(fi);
				AssociationMarkInfo.WriteMarkInfoToElement(independentTag, MarkType.Radiation);
				return;
			}
			document.LoadFamily(UnifiedModified.GetMarkFamilyPath(document, "HY_散热器标记"));
			IndependentTag independentTag2 = RevitVersionFuncs.NewTag(document, document.ActiveView, new Reference(fi), false, 0, tagOrientation, this.GetTagLoca(fi, isUp));
			independentTag2.HasLeader = false;
			independentTag2.TagOrientation = this.GetTagOrientation(fi);
			AssociationMarkInfo.WriteMarkInfoToElement(independentTag2, MarkType.Radiation);
		}

		private XYZ GetTagLocation(FamilyInstance fi, bool isUp = false)
		{
			double num = 0.7;
			XYZ xyz;
			if (Math.Abs(fi.FacingOrientation.Normalize().Y) < 0.001)
			{
				if (fi.FacingOrientation.Normalize().X > 0.0)
				{
					xyz = -num * fi.FacingOrientation.Normalize() * (double)Convert.ToInt16(isUp);
				}
				else
				{
					xyz = num * fi.FacingOrientation.Normalize() * (double)Convert.ToInt16(isUp);
				}
			}
			else if (fi.FacingOrientation.Normalize().Y > 0.0)
			{
				xyz = num * fi.FacingOrientation.Normalize() * (double)Convert.ToInt16(isUp);
			}
			else
			{
				xyz = -num * fi.FacingOrientation.Normalize() * (double)Convert.ToInt16(isUp);
			}
			return ((LocationPoint)fi.Location).Point + xyz;
		}

		private XYZ GetTagLoca(FamilyInstance fi, bool isUp)
		{
			XYZ result = new XYZ();
			XYZ xyz = fi.FacingOrientation.Normalize();
			fi.HandOrientation.Normalize();
			XYZ xyz2 = new XYZ();
			if (this.doc.ActiveView.UpDirection.AngleTo(xyz) > Math.PI*.5 || xyz.AngleTo(this.doc.ActiveView.UpDirection) > Math.PI*.5)
			{
				xyz2 = -xyz;
			}
			else
			{
				xyz2 = xyz;
			}
			if (!isUp)
			{
				result = (fi.Location as LocationPoint).Point - xyz2 * 0.9;
			}
			else
			{
				result = (fi.Location as LocationPoint).Point + xyz2 * 0.9;
			}
			return result;
		}

		private TagOrientation GetTagOrientation(FamilyInstance fi)
		{
			XYZ xyz = fi.FacingOrientation.Normalize();
			TagOrientation result;
			if (xyz.AngleTo(this.doc.ActiveView.UpDirection) <= 0.78539816339744828 || this.doc.ActiveView.UpDirection.AngleTo(xyz) <= 0.78539816339744828 || xyz.AngleTo(this.doc.ActiveView.UpDirection) >= 2.3561944901923448 || this.doc.ActiveView.UpDirection.AngleTo(xyz) >= 2.3561944901923448)
			{
				result = 0;
			}
			else
			{
				result = 1;
			}
			return result;
		}

		private Document doc;
	}
}
